package csc.daonjpa.java.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import csc.daonjpa.java.dao.AccountDAO;
import csc.daonjpa.java.dao.CardDAO;
import csc.daonjpa.java.dao.CustomerDAO;
import csc.daonjpa.java.dao.TargetAccountDAO;
import csc.daonjpa.java.dao.TransactionDAO;
import csc.daonjpa.java.domain.Account;
import csc.daonjpa.java.domain.Card;
import csc.daonjpa.java.domain.Customer;
import csc.daonjpa.java.domain.TargetAccount;
import csc.daonjpa.java.domain.Transaction;

/**
 * This is a controller for home page when user first accesses the application
 * @author nvu3
 *
 */

@Controller
public class HomeController {
	/**
	 * Returns a list of accounts
	 * @param model
	 * @return
	 */
	
	
	@RequestMapping(value = "/home")
	public String getAccountList(HttpServletRequest request,Model model) {
		HttpSession session = request.getSession(true);
				String loginId = (String)session.getAttribute("username");
		// Create a new AccountDAO
		AccountDAO accountDao = new AccountDAO();
		
		// Get the list of all accounts from DB
		List<Account> accountList = accountDao.getAccounts(loginId); 
		
		//Add the list of accounts to request object which will be then used in jsp to show to user
		model.addAttribute("listAccount", accountList);
		return ("home");
	}
	
	@RequestMapping(value = "/loadTransaction")
	public String getTransactionList(Model model){
		TransactionDAO transactionDAO = new TransactionDAO();
		List<Transaction> listTransaction = transactionDAO.getTransactions();
		model.addAttribute("listTransaction",listTransaction);
		
		TargetAccountDAO targetAccountDAO = new TargetAccountDAO();
		List<TargetAccount> listTargetAccount = targetAccountDAO.getTargetAccountList();
		model.addAttribute("listTargetAccount",listTargetAccount);
		return("transaction");
	}
	
	@RequestMapping(value = "/loadCustomer")
	public String doModifyCustomer(HttpServletRequest request, Model model){
		String loginId = request.getParameter("loginId");
		CustomerDAO cusDAO = new CustomerDAO();
		Customer cus = cusDAO.getCustomers(loginId);
		
		model.addAttribute("password",cus.getPassword());
		model.addAttribute("firstname",cus.getFirstname());
		model.addAttribute("lastname",cus.getLastname());
		model.addAttribute("address",cus.getAddress());
		model.addAttribute("phone",cus.getPhone());
		return("modifyCustomer");
	}
}
